<?php
class Material extends Model
{
	/*
	Determines if a given type and weight_per_unit is an Material
	*/
	function exists($type, $weight_per_unit)
	{
		$this->db->from('materials');
		$this->db->where('type',$type);
		$this->db->where('weight_per_unit',$weight_per_unit);
		$query = $this->db->get();

		return ($query->num_rows()==1);
	}
	
	/*
	Determines if a given type and weight_per_unit is an Material
	*/
	function exists_id($material_id)
	{
		$this->db->from('materials');
		$this->db->where('material_id',$material_id);
		$query = $this->db->get();

		return ($query->num_rows()==1);
	}

	/*
	Returns all the Materials
	*/
	function get_all()
	{
		$this->db->from('materials');
		$this->db->order_by("type", "asc");
		$this->db->order_by("weight_per_unit", "asc");
		return $this->db->get();
	}

	/*
	Gets information about a particular Material
	*/
	function get_info($material_id)
	{
		$this->db->from('materials');
		$this->db->where('material_id',$material_id);
		
		$query = $this->db->get();

		if($query->num_rows()==1)
		{
			return $query->row();
		}
		else
		{
			//Get empty base parent object, as $item_id is NOT an material
			$material_obj=new stdClass();

			//Get all the fields from materials table
			$fields = $this->db->list_fields('materials');

			foreach ($fields as $field)
			{
				$material_obj->$field='';
			}

			return $material_obj;
		}
	}

//	/*
//	Gets information about multiple materials
//	*/
//	function get_multiple_info($type)
//	{
//		$this->db->from('materials');
//		$this->db->where('type',$type);
//		$this->db->order_by("weight_per_unit", "asc");
//		return $this->db->get();
//	}

	/*
	Inserts or updates a material
	*/
	function save(&$material_data, $material_id=false )
	{
		$type = $material_data['type'];
		$weight_per_unit = $material_data['weight_per_unit'];
		if (!$this->exists($type, $weight_per_unit))
		{
			if ($this->db->insert('materials',$material_data)) {
				$material_data['material_id']=$this->db->insert_id();
				return true;
			}
			return false;
		}
		$this->db->where('type', $material_data['type']);
		$this->db->where('weight_per_unit', $material_data['weight_per_unit']);
		return $this->db->update('materials',$material_data);
	}

	/*
	Deletes one mataerial
	*/
	function delete($material_id)
	{
		return $this->db->delete('materials', array('material_id' => $material_id));
	}

	/*
	Deletes a list of mataerials
	*/
	function delete_list($material_ids)
	{	
		$return_flag = true;
		foreach ( $material_ids as $material_id) {
			$return_flag = $return_flag and ($this->db->delete('materials', array('material_id' => $material_id )));
		}
		return $return_flag;
 	}

 	
	/*
	Preform a search on items
	*/
	function search($search)
	{
		$this->db->from('materials');
		$this->db->where("type LIKE '%".$this->db->escape_like_str($search)."%'  or 
		weight_per_unit LIKE '".$this->db->escape_like_str($search)."'");
		$this->db->order_by("type", "asc");
		return $this->db->get();	
	}
	
	/*
	Get search suggestions to find items
	*/
	function get_search_suggestions($search,$limit=25)
	{
		$suggestions = array();

		$this->db->from('materials');
		$this->db->like('type', $search);
		$this->db->order_by("type", "asc");
		$this->db->group_by('type');
		$by_type = $this->db->get();
		foreach($by_type->result() as $row)
		{
			$suggestions[]=$row->type;
		}
		
		$this->db->from('materials');
		$this->db->like('weight_per_unit', $search);
		$this->db->order_by("weight_per_unit", "asc");
		$this->db->group_by('weight_per_unit');
		$by_weight_per_unit = $this->db->get();
		foreach($by_weight_per_unit->result() as $row)
		{
			$suggestions[]=$row->weight_per_unit;
		}

		//only return $limit suggestions
		if(count($suggestions > $limit))
		{
			$suggestions = array_slice($suggestions, 0,$limit);
		}
		return $suggestions;

	}



}
?>
